import Vue from 'vue'

const router = {
	navigateTo(url, data = {}) {
		Vue.prototype.$pageData = data
		return new Promise((resolve, reject) => {
			uni.navigateTo({
				url: url,
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error('页面跳转异常', err)
					reject(err)
				}
			})
		})
	},
	redirectTo(url, data = {}) {
		Vue.prototype.$pageData = data
		return new Promise((resolve, reject) => {
			uni.redirectTo({
				url: url,
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error('页面跳转异常', err)
					reject(err)
				}
			})
		})
	},
	reLaunch(url, data = {}) {
		Vue.prototype.$pageData = data
		return new Promise((resolve, reject) => {
			uni.reLaunch({
				url: url,
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error('页面跳转异常', err)
					reject(err)
				}
			})
		})
	},
	switchTab(url, data = {}) {
		Vue.prototype.$pageData = data
		return new Promise((resolve, reject) => {
			uni.switchTab({
				url: url,
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error('页面跳转异常', err)
					reject(err)
				}
			})
		})
	},
	back(num = 1) {
		uni.navigateBack({
			delta: num
		})
	}
}

Vue.prototype.$router = router
